/*
 * @lc app=leetcode.cn id=169 lang=typescript
 *
 * [169] 多数元素
 */

// @lc code=start
function majorityElement(nums: number[]): number {
    if (nums.length === 1) return nums[0];
    let a = new Map<number, number>();
    for (let index = 0; index < nums.length; index++) {
        const element = nums[index];
        if (a.has(element)) {
            a.set(element, a.get(element)! + 1);
        } else {
            a.set(element, 1);
        }
    }
    const result = [...a.entries()].reduce(
        ([key1, val1], [key, val]) => {
            if (val > val1) {
                return [key, val];
            } else {
                return [key1, val1];
            }
        },
        [0, 0] as [number, number]
    );
    return result[0];
}
// @lc code=end
